<?php  
	/** 
	 * 公共样例Controller 后台公用类
	 * @author wxy
	 * 
	 */  
	class exampleController extends adminController {

		var $pageNum;
		
		public function __construct(){
			parent::__construct();
			$this->exampleService = InitPHP::getService("example"); 			//实例化service层
			$this->pageNum = 10;       										//每页显示条数

			//Action白名单,用于可以再外部用url访问，形式为参数a的值
			$this->initphp_list[] = 'example_list';
			$this->initphp_list[] = 'example_add';
			$this->initphp_list[] = 'example_edit'; 
			$this->initphp_list[] = 'uploadImg';
			$this->initphp_list[] = 'example_dialist';
		}

	    /**
	     * common列表
	     * by wxy
	     */
	    public function example_list(){
	    	//$page = intval($this->controller->get_gp('page'));
	    	//list($list, $count) = $this->getService("example")->getExampleList(array(), $this->pageNum, $page);
	    	
	    	//删除记录
	    	$action = $this->controller->get_gp("action");
	    	if($action == 'del'){
	    		$did = $this->controller->get_gp("id");
	    		//以下组合 in (1,1,3,4）条件
	    		$didArr = explode(',',$did);
	    		$fields = array('id' => $didArr);
	    		$res = $this->exampleService->exampleDel($fields);
	    		$msg = '删除失败';
	    		$statusCode = '300';
	    		if($res){
	    			$msg = '删除成功';
	    			$statusCode = '200';
	    		}
	    		die($this->ajaxDone($msg, $statusCode));
	    	}

	    	//动态修改
	    	if($action == 'edit'){
	    		$title = $this->controller->get_gp("vals");
	    		$id = $this->controller->get_gp("id");
	    		$arr = array('title' => $title);
	    		$res = $this->exampleService->exampleUpdate($arr, array('id' => $id));
	    		die($res);
	    	}

	    	$field = array();
	    	if($_POST['title'] != ''){
	    		$field['title'] = array("like" => '%'.$_POST['title'].'%');
	    	}
	    	if($_POST['state'] != ''){
	    		$field['state'] = $_POST['state'];
	    	}
	    	$this->view->assign('search', $_POST);

	    	$list = $this->exampleService->getExampleList($field, $this->pageNum);
	    	$this->view->assign('list', $list['result']);
	    	$this->view->assign('page', $list['page']);
	    	//魔板显示 view api
	    	$this->view->display("example_list");   
	    } 

	    /**
	     * 修改密码
	     * @return 
	     * @author wxy 
	     */
	    function example_add(){
	    	if($_POST){
	    		include_once "uploadImg.class.php";
	    		if($_FILES['img']['name'] != ''){
	    			$upimg = new uploadImg();

	    			$path = '../app/data/upload/album/';
	    			$img = $upimg->upLoad("img", $path); 
	    		}
	    		$realname = $_POST['realname'];
	    		$arr = array(
	    			'title' => $_POST['title'],
	    			'img' => $img,
	    			'realname' => $realname,
	    			'date' => time(),
	    			'age' => $_POST['age'],
	    			'intro' => $_POST['intro']
	    			);
	    		$nid = $this->exampleService->exampleAdd($arr);
	    		if($nid > 0){
	    			$msg = '添加成功';
	    			$statusCode = '200';
	    		}else{
	    			$msg = '添加失败';
	    			$statusCode = '300';
	    		}
	    		$url = 'admin.php?c=example&a=example_edit&id='.$nid;
	    		die($this->ajaxDone($msg , $statusCode, $url));
	    	}

	    	$type = $this->controller->get_gp("type");

	    	if($type == 'dialog'){
	    		$cid = $this->controller->get_gp("cid");
	    		$this->view->assign("cid", $cid);
	    	}

	    	$html = $type == 'dialog' ? 'example_d' : 'example_add';
	    	
	    	$this->view->display($html);  
	    } 

	    /**
	     * 修改
	     * @return [type] [description]
	     */
	    function example_edit(){
	    	//$id1 = $_GET['id'];
	    	$id = $this->controller->get_gp("id");
	    	$type = $this->controller->get_gp("type");   //弹窗刷新父级页面用
	    	$this->view->assign("type",$type);

	    	//详情
	    	$info = $this->exampleService->getExampleOne($id);
	    	$img = $info['img'];

	    	if($_POST){
	    		include_once "uploadImg.class.php";
	    		if($_FILES['img']['name'] != ''){
	    			$upimg = new uploadImg();

	    			$path = '../app/data/upload/album/';
	    			@unlink($path.$img);
	    			$img = $upimg->upLoad("img", $path); 
	    		}
	    		$realname = $_POST['realname'];
	    		$arr = array(
	    			'title' => $_POST['title'],
	    			'img' => $img,
	    			'realname' => $realname,
	    			'date' => time(),
	    			'age' => $_POST['age'],
	    			'intro' => $_POST['intro']
	    			);
	    		$field = array('id' => $id);
	    		$res = $this->exampleService->exampleUpdate($arr, $field);
	    		if($res){
	    			$msg = '修改成功';
	    			$statusCode = '200';
	    		}else{
	    			$msg = '修改失败';
	    			$statusCode = '300';
	    		}
	    		if($type == 'dialog'){
	    			$forwardUrl = 'admin.php?c=example&a=example_list';
	    			die($this->ajaxDone($msg , $statusCode, $forwardUrl, "closeCurrent", "0_a_1"));
	    		}
	    		die($this->ajaxDone($msg , $statusCode));	
	    	}
	    	//echo $id1.'>>>'.$id; 
	    	$this->view->assign("info",$info);
	    	$this->view->display("example_edit");  
	    }

	    //相册上传多图
	    function uploadImg(){
	    	$action = $this->controller->get_gp("action");
	    	
	    	$folder = "../app/data/upload/";
			$annexFolder = $folder."album/";
			//$middleFolder = $folder."album/";
			
			if($action == 'upload'){
				//上传新图
				include_once "uploadImg.class.php";
				$upimg = new uploadImg();
				$img = $upimg->upLoad('image',$annexFolder);
				//中图
				//$upimg->smallImg($annexFolder.$img,$middleFolder);
			
				echo json_encode(array('fileName'=>$img));
				exit();
	    	}
			
			if($_GET['action']=='delFile'){
				//unlink($_POST['file']);
				$img=$_POST['file'];
				//删除旧图
				@unlink($annexFolder.$img);
				//@unlink($middleFolder.$img);
				//@unlink($smallFolder.$img);
				exit();
			}
	    }

	    /**
	     * 弹窗列表
	     * @return [type] [description]
	     */
	    function example_dialist(){
	    	$field = array();
	    	if($_POST['title'] != ''){
	    		$field['title'] = array("like" => '%'.$_POST['title'].'%');
	    	}
	    	if($_POST['state'] != ''){
	    		$field['state'] = $_POST['state'];
	    	}
	    	$this->view->assign('search', $_POST);

	    	$list = $this->exampleService->getExampleList($field, $this->pageNum);

	    	if($_POST['submits']){
	    		$idArr = $this->controller->get_gp("id");
	    		$ids = implode(",",$idArr);
	    		$forwardUrl = 'admin.php?c=example&a=example_add&type=dialog&cid='.$ids;
	    		$msg = 'OK';
	    		echo $this->ajaxDone($msg,'200',$forwardUrl,'closeCurrent','0_a_1');
	    		exit();
	    	}
	    	$this->view->assign('list', $list['result']);
	    	$this->view->assign('page', $list['page']);
	    	$this->view->display("example_dialist"); 
	    }

	}  
?>